package net.wanglu.www.zzz.service.se.obj;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import net.wanglu.www.zzz.service._Service;
import net.wanglu.www.zzz.service.se.__SentenceElements;
import edu.stanford.nlp.trees.EnglishGrammaticalRelations;
import edu.stanford.nlp.trees.GrammaticalRelation;
import edu.stanford.nlp.trees.TypedDependency;

public class _ObjectExtracter extends _Service {

	private _Service dObjExtracter;
	private _Service iObjExtracter;
	private _Service pObjExtracter;
	public _Service getdObjExtracter() {
		return dObjExtracter;
	}
	public void setdObjExtracter(_Service dObjExtracter) {
		this.dObjExtracter = dObjExtracter;
	}
	public _Service getiObjExtracter() {
		return iObjExtracter;
	}
	public void setiObjExtracter(_Service iObjExtracter) {
		this.iObjExtracter = iObjExtracter;
	}
	public _Service getpObjExtracter() {
		return pObjExtracter;
	}
	public void setpObjExtracter(_Service pObjExtracter) {
		this.pObjExtracter = pObjExtracter;
	}

	@Override
	public Object service(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		HashMap<?, ?> preprocessData = (HashMap<?, ?>) objects[0];
		__SentenceElements se = null;
		if ( objects.length >1) {
			se = (__SentenceElements) objects[1];
		} else {
			se = new __SentenceElements();
		}
		
		ArrayList<_ObjectTuple> ObjectTupleList = new ArrayList<_ObjectTuple>(8);
		Collection<?> tdl = (Collection<?>) preprocessData.get("tdl");
		for( Object obj : tdl) {
			TypedDependency TD = (TypedDependency) obj;
			GrammaticalRelation reln = TD.reln();
			if ( reln.equals(EnglishGrammaticalRelations.DIRECT_OBJECT)) {
				/*
				 * 1.调用dobj解析器
				 * 2.将结果添加到ObjectTupleList
				 */
				_ObjectTuple ot = (_ObjectTuple) this.dObjExtracter.service(preprocessData, TD);
				if ( ot != null) ObjectTupleList.add(ot);
			} else if ( reln.equals(EnglishGrammaticalRelations.INDIRECT_OBJECT)) {
				/*
				 * 1.调用idobj解析器
				 * 2.将结果添加到ObjectTupleList
				 */
				_ObjectTuple ot = (_ObjectTuple) this.iObjExtracter.service(preprocessData, TD);
				if ( ot != null) ObjectTupleList.add(ot);
			} else if ( reln.equals(EnglishGrammaticalRelations.PREPOSITIONAL_OBJECT)) {
				/*
				 * 1.调用pobj解析器
				 * 2.将结果添加到ObjectTupleList
				 */	
				_ObjectTuple ot = (_ObjectTuple) this.pObjExtracter.service(preprocessData, TD);
				if ( ot != null) ObjectTupleList.add(ot);
			}
		}
		
		se.ObjectTupleList.addAll(ObjectTupleList);
		return se;
	}
}
